<template>
	<view>
		<view class="address" @click="getaddress" >
			<view class="addinfo" v-if="choose_address">
				<view class="addinfodd" v-if="choose_address.ad1">
					{{choose_address.ad1}}
					{{choose_address.ad2}}
					{{choose_address.ad3}}
					{{choose_address.detail}}
				</view>
				<view class="addinfodd" v-if="choose_address.length == 0">
					<view class="uinfo" style="padding-top: 20px;">请选择收货地址</view>
				</view>
				<view class="uinfo">
					<view style="margin-right: 20px;">{{choose_address.rec_user_name}}</view>
					<view>{{choose_address.rec_mobile}}</view>
				</view>
			</view>
			<view class="addinfo2" v-else>
				<view class="addinfodd"></view>
				<view class="uinfo" style="padding-top: 20px;">请完善您的收货地址信息</view>
			</view>
			<view class="riicon" >
				<image src="https://ayh.wissun.net/static/newhome/img/riicon.png" />
			</view>
		</view>
		
		<view class="goodsinfo">
			<image :src="datalist.goods_info.goods_pic" class="showpic" />
			<view class="goodsname">
				<view>{{datalist.goods_info.goods_name}}</view>
				<view class="prris">
					<view>
						<view v-if="datalist.goods_info.integral">{{datalist.goods_info.integral}}积分</view>
						<block v-if="datalist.goods_info.amount">
							<view class="money">￥{{datalist.goods_info.amount}}</view>
						</block>
					</view>
					<view>x1</view>
				</view>
			</view>
		</view>
		<block v-if="datalist.goods_info.amount">
			<view class="goods_pirce">
				<view>商品金额：</view>
				<view class="pdright" >{{datalist.goods_info.amount}}元</view>
			</view>
		</block>
		
		<view class="goods_pirce" >
			<view>所需积分：</view>
			<view class="pdright">{{datalist.goods_info.integral}}积分</view>
		</view>
		
		<block v-if="isclick">
			<view id="zhifu"  class="zhifu">确定</view>
		</block>
		
		<block v-if="!isclick">
			<view id="zhifu" @click="wxpay" class="zhifu">确定</view>
		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datalist:[],
				openid:"",
				user_id:0,
				goods_id:0,
				choose_address:[],
				again:0
			}
		},
		onLoad(p) {
			var that=this;
			var data={}
			data.user_id=p.user_id
			data.goods_id=p.goods_id
			this.getdata(data)
			this.goods_id=p.goods_id
			this.user_id=p.user_id
		},
		 onUnload(){
			this.isclick=0
		},
		onShow() {
			this.isclick=0
			var choose_address=uni.getStorageSync('choose_address')
			if(choose_address){
				this.choose_address=JSON.parse(choose_address)
			}
		},
		methods: {
			getdata(data){
				var that=this;
				uni.setStorageSync('comfirm_data', data);
				var that=this;
				var postdata={}
				postdata.openid=uni.getStorageSync("openid")
				this.request('/api/integral_shop_front/confirm_order_integral', data,"post").then(data => {
					that.datalist=data.info.data
				})
			},
			getaddress(){
				uni.navigateTo({
				  url: '/pages/address/choose',
				})
			},
			//确认提交
			wxpay(){
				var that = this;
				if(that.isclick > 0){
					return;
				}
				uni.showToast({  
					title: '正在请求...',  
					icon: 'loading',
					mask: true,  
					duration: 10000  
				})
				var openid=uni.getStorageSync('openid')
				this.isclick=1
				var data={}
				data.user_code=uni.getStorageSync("fahuo_username").user_code;
				data.user_id=this.user_id
				data.goods_id=this.goods_id
				data.open_id=openid
				data.address_id=this.choose_address.id				
				if(this.choose_address.length == 0){
					uni.hideLoading()
					uni.showToast({ title: '请完善收货信息后再来下单',icon:'none' })
					setTimeout(function(){
					   that.getaddress()
					},1500)
					return
				}
				if(this.choose_address){
					var that=this
				    this.request('/api/integral_shop_front/get_order_json_myhy', data,"post").then(ret => {
						that.isclick=0;
				   	    //code 0 积分兑换 1微信支付 2错误提示
				   	    if(ret.info.code == 1){
						  uni.hideLoading()
						  var json=ret.info.data
						  uni.requestPayment({
							timeStamp: json.timeStamp,
							nonceStr: json.nonceStr,
							package: json.package,
							signType:json.signType,
							paySign: json.paySign,
							success (res) { 
								uni.showToast({ title: '订单提交成功',})
								setTimeout(function(){
									uni.redirectTo({
									  url: '/pages/main/duojifahuo/shop/order',
									})
								},2000)
							},
							fail (res) { }
						  })
				   	    }else if(ret.info.code == 0){
						  uni.hideLoading()
						  uni.showToast({
							title: ret.info.msg,
						  })
						  setTimeout(() => {
							uni.navigateBack({ delta: 1 })
						  }, 2000);
				   	    }else if(ret.info.code == 2){
						  uni.hideLoading()
						  uni.showToast({
						  	icon:"none",
						  	title: ret.info.msg
						  })
				   	    }
				    })
				}else{
					 uni.hideLoading()
					 uni.showToast({
						title: '请完善收货信息后再来下单',
						icon:'none'
					  })
					  setTimeout(function(){
						that.getaddress()
					  },2000)
					  return
				}
			},
		}
	}
</script>

<style>
.address{
    width: calc(100%  - 40px);
    margin: 0 auto;
    height: 60px;
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    background-color: #fff;
    font-size: 14px;
    padding: 10px;
    margin-top: 10px;
}
.showtips{font-size: 12px;color: #999;padding: 10px;}
.addinfo{width: calc(100% - 40px);line-height: 26px;}
.riicon{width: 20px;padding-top: 26px;}
.riicon image{width: 20px;height: 20px;}
page{background: #eee;}
.uinfo{padding-top: 6px;color: #444141;display:flex;}
.goodsinfo{
    background: #fff;
    border-radius: 10px 10px 0px 0px;
    display: flex;
    width: calc(100% - 20px);
    margin: 0 auto;
    margin-top: 10px;
}
.showpic{
    padding: 20px;
    width: 80px;
    height: 80px;
    border-radius: 25px;
}
.goodsname{
    font-size: 14px;
    margin-top: 15px;
    position: relative;

}
.prris{
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
}
.money{color: red;}
.goods_pirce{
    height: 40px;
    line-height: 40px;
    background: #fff;
    padding-left: 20px;
    display: flex;
    border-bottom: 1px solid #eee;
    justify-content: space-between;
    font-size: 14px;
    width: calc(100% - 40px);
    margin: 0 auto;
}
.goods_pirce span{}
.pdright{padding-right: 10px;}
.zhifu{
    height: 40px;
    width: 80%;
    text-align: center;
    background: #3078b1;
    margin: 0 auto;
    color: #fff;
    border-radius: 10px;
    line-height: 40px;
    margin-top: 100px;
}
</style>
